package com.pr.makfilm.user.pages.school.list.cmp;

import com.pr.makfilm.core.cmp.notify.Notification;
import com.pr.makfilm.core.cmp.web.BootstrapDropDownPanel;
import com.pr.makfilm.core.cmp.web.BootstrapTextFieldPanel;
import com.pr.makfilm.core.cmp.window.AbstractWindow;
import com.pr.makfilm.domain.District;
import com.pr.makfilm.domain.School;
import com.pr.makfilm.domain.SchoolType;
import com.pr.makfilm.service.ISchoolService;
import com.pr.makfilm.service.ISchoolTypeService;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.StringValidator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 23.09.13
 * Time: 23:27
 * To change this template use File | Settings | File Templates.
 */
public abstract class SchoolWindow extends AbstractWindow<School>
{
   @SpringBean
   private ISchoolTypeService schoolTypeService;

   @SpringBean
   private ISchoolService schoolService;

   private boolean createMode = true;

   private List<District> districts = new ArrayList<District>();

   private List<SchoolType> types = new ArrayList<SchoolType>();

   public SchoolWindow(String id)
   {
      super(id, Model.of(new School()), new Model<String>());
   }

   @Override
   protected void onConfigure()
   {
      types = schoolTypeService.findUserTypes();
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      BootstrapTextFieldPanel name = new BootstrapTextFieldPanel("name",
              new PropertyModel(this, "school." + School.FIELD_NAME));
      name.setValidation(true);
      name.getField().add(StringValidator.maximumLength(School.MAX_LENGTH_NAME));
      form.add(name);

      BootstrapDropDownPanel<SchoolType> type = new BootstrapDropDownPanel<SchoolType>("type",
              new PropertyModel<SchoolType>(this, "school." + School.FIELD_TYPE),
              new PropertyModel<List<SchoolType>>(this, "types"), new IChoiceRenderer<SchoolType>()
      {
         @Override
         public Object getDisplayValue(SchoolType object)
         {
            return object.getName();
         }

         @Override
         public String getIdValue(SchoolType object, int index)
         {
            return object.getSid();
         }
      });
      type.setValidation(true);
      form.add(type);

      BootstrapDropDownPanel<District> district = new BootstrapDropDownPanel<District>("district",
              new PropertyModel<District>(this, "school." + School.FIELD_DISTRICT),
              new PropertyModel<List<District>>(this, "districts"), new IChoiceRenderer<District>()
      {
         @Override
         public Object getDisplayValue(District object)
         {
            return object.getName();
         }

         @Override
         public String getIdValue(District object, int index)
         {
            return object.getSid();
         }
      });
      district.setValidation(true);
      form.add(district);

      BootstrapTextFieldPanel city = new BootstrapTextFieldPanel("city",
              new PropertyModel(this, "school." + School.FIELD_CITY));
      city.setValidation(true);
      city.getField().add(StringValidator.maximumLength(School.MAX_LENGTH_CITY));
      form.add(city);

      BootstrapTextFieldPanel street = new BootstrapTextFieldPanel("street",
              new PropertyModel(this, "school." + School.FIELD_STREET));
      street.setValidation(true);
      street.getField().add(StringValidator.maximumLength(School.MAX_LENGTH_STREET));
      form.add(street);

      BootstrapTextFieldPanel zip = new BootstrapTextFieldPanel("zip",
              new PropertyModel(this, "school." + School.FIELD_ZIP));
      zip.setValidation(true);
      zip.getField().add(StringValidator.maximumLength(School.MAX_LENGTH_ZIP));
      form.add(zip);
   }

   @Override
   public void onSubmit(AjaxRequestTarget target, Form<?> form)
   {
      School school = getSchool();
      if (createMode())
      {
         schoolService.save(school);
         Notification.success(getString("school.successfully.saved"));
      }
      else
      {
         School old = schoolService.getBySid(school.getSid());
         schoolService.update(old);
         Notification.success(getString("school.successfully.updated"));
      }
   }

   public void resetState(boolean createMode)
   {
      this.createMode = createMode;
      if (createMode)
      {
         setSchool(new School());
      }
      setHeader(createMode ? getString("create.title") : getString("edit.title"));
   }

   public boolean createMode()
   {
      return createMode;
   }

   public School getSchool()
   {
      return (School) getDefaultModelObject();
   }

   public void setSchool(School school)
   {
      setDefaultModelObject(school);
   }

   public void setDistricts(List<District> districts)
   {
      this.districts = districts;
   }
}
